# application/services/detection/product_coil.py
from application.mapper.detection.product_coil import ProductCoilMapper
from typing import Dict, Any, List, Tuple
from application.models.detection.product_coil import ProductCoil
from application.utils.common import process_array_fields, set_model_attributes
from application.utils.res import models_to_dicts_camel, to_snake_case


class ProductCoilService:
    """绕组服务层"""

    @staticmethod
    def get_product_coil_list(
        page_num: int, page_size: int, conditions: Dict[str, Any] = None
    ) -> Tuple[List[Dict[str, Any]], int]:
        """
        分页获取绕组数据列表
        :param page_num: 页码
        :param page_size: 每页数量
        :param conditions: 查询条件
        :return: (绕组数据列表, 总数)
        """
        coil_list, total = ProductCoilMapper.get_product_coil_list(
            page_num, page_size, conditions
        )

        result_list = models_to_dicts_camel(coil_list)

        return result_list, total

    @staticmethod
    def get_product_coil_by_id(coil_id: int) -> Dict[str, Any]:
        """
        根据ID获取绕组详情
        :param coil_id: 绕组ID
        :return: 绕组数据
        """
        coil = ProductCoilMapper.get_product_coil_by_id(coil_id)

        if not coil:
            return None

        return models_to_dicts_camel([coil])[0]

    @staticmethod
    def get_coils_by_product_model(product_model_id: int) -> List[Dict[str, Any]]:
        """
        根据产品型号ID获取绕组列表
        :param product_model_id: 产品型号ID
        :return: 绕组列表
        """
        coils = ProductCoilMapper.get_coils_by_product_model(product_model_id)
        return models_to_dicts_camel(coils)

    @staticmethod
    def create_product_coil(coil_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        创建绕组
        :param coil_data: 绕组数据
        :return: 创建后的绕组数据
        """
        # 处理数组字段
        processed_data = process_array_fields(
            coil_data,
            [
                "voltageFullLoad",
                "voltageLightLoad",
            ],
        )

        # 创建绕组对象
        coil = ProductCoil()

        # 设置对象属性
        coil = set_model_attributes(coil, processed_data)

        created_coil = ProductCoilMapper.create_product_coil(coil)

        return models_to_dicts_camel([created_coil])[0]

    @staticmethod
    def update_product_coil(coil_id: int, coil_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        更新绕组
        :param coil_id: 绕组ID
        :param coil_data: 绕组数据
        :return: 更新后的绕组数据
        """
        coil = ProductCoilMapper.get_product_coil_by_id(coil_id)

        if not coil:
            return None

        # 处理数组字段
        processed_data = process_array_fields(
            coil_data,
            [
                "voltageFullLoad",
                "voltageLightLoad",
            ],
        )

        # 过滤掉ID字段，避免尝试更新主键
        coil = set_model_attributes(coil, processed_data, exclude_fields=["coilid"])

        updated_coil = ProductCoilMapper.update_product_coil(coil)
        return models_to_dicts_camel([updated_coil])[0]

    @staticmethod
    def batch_delete_product_coil(coil_ids: list) -> bool:
        """
        批量删除绕组
        :param coil_ids: 绕组ID列表
        :return: 删除的记录数
        """
        deleted_count = ProductCoilMapper.batch_delete_product_coil(coil_ids)
        return deleted_count
